package cn.bgs.wjx.WjxController;

import cn.bgs.ActivityManagement.pojo.Announcement;
import cn.bgs.CommunityManagement.pojo.*;
import cn.bgs.DealershipManagement.pojo.VehicleCar;
import cn.bgs.UserManagement.pojo.File;
import cn.bgs.UserManagement.pojo.User;
import cn.bgs.UserManagement.pojo.VisitorsRecord;
import cn.bgs.utils.HttpUtil;
import cn.bgs.utils.R;
import cn.bgs.wjx.WjxService.lmpl.WjxServicelmpl;
import cn.bgs.wjx.pojo.AliOss;
import cn.bgs.wjx.pojo.CellIntermediateWvo;
import cn.bgs.wjx.pojo.VehicleCarWvo;
import cn.bgs.wjx.pojo.VisitorsRecordWvo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.stream.Collectors;

@RestController
public class WjxController {
    @Autowired
    WjxServicelmpl wjxServicelmpl;
    @Autowired
    AliOss aliOss;
    @Autowired
    JavaMailSender javaMailSender;

//    app端登陆
    @RequestMapping("/WjxLogin")
    public R WjxLogin(@RequestBody User user){
        User user1=wjxServicelmpl.WjxLogin(user);
        return R.ok().data("user",user1);
    }
//    我的默认房屋
    @RequestMapping("/WjxFindDeaultState")
    public R WjxFindMyDeaultState(Integer userId){
        CellIntermediateWvo defaultState = new CellIntermediateWvo();
        List<CellIntermediateWvo> wvo= (List<CellIntermediateWvo>) WjxFindDeaultState(userId,0);
        List<CellIntermediateWvo> WvoState = wvo.stream().filter(e -> e.getDefaultState().equals("是")).collect(Collectors.toList());
        for (CellIntermediateWvo cellIntermediateWvo : WvoState) {
            defaultState=cellIntermediateWvo;
        }
        if (defaultState.getMiddleId()!=null){
            String address=defaultState.getCommunityName()+"小区"+defaultState.getHowMany()+"楼"+defaultState.getHowFloor()+"层"+defaultState.getHowPortal();
            System.out.println("ll:"+address);
            Map map = new HashMap();
            map.put("middleId",defaultState.getMiddleId());
            map.put("address",address);
            return R.ok().code(2000).data(map);
        }else {
        return R.error().code(5000);
        }
    }
//    添加访客记录
    @RequestMapping("/WjxAddVisitorsRecord")
    public R WjxAddVisitorsRecord(@RequestBody VisitorsRecord visitorsRecord){
        wjxServicelmpl.WjxAddVisitorsRecord(visitorsRecord);
        System.out.println("--:"+visitorsRecord);
       return R.ok().code(2000).data("visitorsRecord",visitorsRecord);
    }
//    查询访客记录
    @RequestMapping("/WjxRecorddetails")
    public R WjxRecorddetails(Integer userId,String startTime,String endTime){
        List<VisitorsRecordWvo> visitorsRecords=wjxServicelmpl.WjxRecorddetails(userId,startTime,endTime);
        List<CellIntermediateWvo> list=new ArrayList<CellIntermediateWvo>();
        for (VisitorsRecordWvo visitorsRecord : visitorsRecords) {
//            拿到小区中间表id
            list.add((CellIntermediateWvo) WjxFindDeaultState(userId,visitorsRecord.getMiddleId()));
        }
        //外层集合是查出用户 下的所有的小区
        for (CellIntermediateWvo cellIntermediateWvo : list) {
            for (VisitorsRecordWvo visitorsRecord : visitorsRecords) {
                if (visitorsRecord.getMiddleId()==cellIntermediateWvo.getMiddleId()){
                   visitorsRecord.setAddress(cellIntermediateWvo.getCommunityName()+"小区"+cellIntermediateWvo.getHowMany()+"楼"+cellIntermediateWvo.getHowFloor()+"层"+cellIntermediateWvo.getHowPortal());
                }
            }
        }

        return R.ok().data("visitorsRecords",visitorsRecords);
    }
    //查询我的房屋
    @RequestMapping("/WjxFindMyHome")
    public R WjxFindMyHome(Integer userId){
        System.out.println("WjxFindDeaultState(userId,0):::"+WjxFindDeaultState(userId,0));
        List<CellIntermediateWvo> wvos= (List<CellIntermediateWvo>) WjxFindDeaultState(userId,0);
        for (CellIntermediateWvo wvo : wvos) {
            wvo.setHowPortal(wvo.getHowMany()+"楼"+wvo.getHowFloor()+"层"+wvo.getHowPortal());
        }
        return R.ok().data("wvos",wvos);
    }

    @RequestMapping("/upload")
    public String upload(MultipartFile file,Integer id){
        String file1=aliOss.upload(file);
        System.out.println(file1);
        File file2 = new File();
        file2.setFileId(0);
        file2.setFile(file1);
        //用户、小区、车辆
        file2.setAssociatedId(id);
        file2.setType("身份证");
        wjxServicelmpl.WjxinsertFile(file2);
        return file1;
    }

    @RequestMapping("/uploadcl")
    public String uploadcl(MultipartFile file){
        return aliOss.upload(file);
    }
    /*识别身份证*/
    @RequestMapping("/card1")
    public R card1(String url1){
        System.out.println("qqqqqq"+url1);
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
        try {

            String param = "id_card_side=" + "front" + "&url=" + url1;
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.500f37463f9f53670bc83656c30e0172.2592000.1657691938.282335-26192645";
            //调用工具类发请求
            String result = HttpUtil.post(url, accessToken, param);
            return R.ok().data("file",result);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().code(5000);
        }
    }
    //查询 小区、楼栋、楼层、门户
    @RequestMapping("/WjxFindAllCommunity")
    public R WjxFindAllCommunity(){
        List<Community> list=wjxServicelmpl.WjxFindAllCommunity();
        return R.ok().data("Community",list);
    }
    @RequestMapping("/WjxFindAllBuilding")
    public R WjxFindAllBuilding(){
        List<Building> list=wjxServicelmpl.WjxFindAllBuilding();
        return R.ok().data("Building",list);
    }
    @RequestMapping("/WjxFindAllFloor")
    public R WjxFindAllFloor(){
        List<Floor> list=wjxServicelmpl.WjxFindAllFloor();
        return R.ok().data("Floor",list);
    }
    @RequestMapping("/WjxFindAllPortal")
    public R WjxFindAllPortal(){
        List<Portal> list=wjxServicelmpl.WjxFindAllPortal();
        return R.ok().data("Portal",list);
    }
    //fsyzm
    @RequestMapping("/fsyzm")
    public R fsyzm(String email){
        try{
            //随机四位数
            String verificationCode = String.valueOf((int)((Math.random()*9+1)*1000));
            // 构建一个邮件对象
            SimpleMailMessage message = new SimpleMailMessage();
            // 设置邮件主题
            message.setSubject("添加房屋验证码");
            // 设置邮件发送者，这个跟application.yml中设置的要一致
            message.setFrom("2435706194@qq.com");
            // 设置邮件接收者，可以有多个接收者，中间用逗号隔开，以下类似
            // message.setTo("10*****16@qq.com","12****32*qq.com");
            message.setTo(email);
            // 设置邮件发送日期
            message.setSentDate(new Date());
            // 设置邮件的正文
            message.setText("你这次的验证码为："+verificationCode+"发送时间："+new Date());
            // 发送邮件
            javaMailSender.send(message);

            return R.ok().code(20000).data("yzm",verificationCode);
        }catch (MailException e){
            e.printStackTrace();
            System.out.println("网络异常");
            return R.ok().code(50000).message("网络异常");
        }
    }
    @RequestMapping("/addCellIntermediate")
    public R addCellIntermediate(@RequestBody CellIntermediateWvo cellIntermediate){
        try {
            wjxServicelmpl.cellIntermediate(cellIntermediate);
            return R.ok().code(20000);
        }catch (Exception e){
            return R.ok().code(50000);
        }

    }

    @RequestMapping("/UpdateCellIntermediate")
    public R UpdateCellIntermediate(@RequestBody CellIntermediateWvo cellIntermediate){
        System.out.println("---:"+cellIntermediate);
        try {
            wjxServicelmpl.UpdateCellIntermediate(cellIntermediate);
            return R.ok().code(20000);
        }catch (Exception e){
            return R.ok().code(50000);
        }

    }


    //查询我的车辆
    @RequestMapping("/WjxFindVehicleCar")
    public R WjxFindVehicleCar(Integer userId){
        return R.ok().data("VehicleCar",wjxServicelmpl.WjxFindVehicleCar(userId));
    }
    //添加我的车辆
    @RequestMapping("/WjxAddVehicleCar")
    public R WjxAddVehicleCar(@RequestBody VehicleCarWvo car){
        try {
            wjxServicelmpl.WjxAddVehicleCar(car);
            return R.ok().code(2000);
        }catch (Exception e){
            e.printStackTrace();
            return R.error().code(5000);
        }
    }


    //修改我的车辆
    @RequestMapping("/WjxUpdateVehicleCar")
    public R WjxUpdateVehicleCar(@RequestBody VehicleCarWvo carWvo){
        try {
            wjxServicelmpl.WjxUpdateVehicleCar(carWvo);
            return R.ok().code(2000);
        }catch (Exception e){
            e.printStackTrace();
            return R.error().code(5000);
        }
    }

    //删除我的车辆
    @RequestMapping("/WjxDeleteVehicleCar")
    public R WjxDeleteVehicleCar(@RequestBody VehicleCarWvo carWvo){
        try {
            wjxServicelmpl.WjxDeleteVehicleCar(carWvo);
            return R.ok().code(2000);
        }catch (Exception e){
            e.printStackTrace();
            return R.error().code(5000);
        }
    }
    //购买月卡
    @RequestMapping("/WjxAddMonthCar")
    public R WjxAddMonthCar(@RequestBody VehicleCarWvo carWvo){
        System.out.println("----:"+carWvo);
        try {
            wjxServicelmpl.WjxAddMonthCar(carWvo);
            return R.ok().code(2000);
        }catch (Exception e){
            e.printStackTrace();
            return R.error().code(5000);
        }
    }

    //修改房屋信息
    @RequestMapping("/getUserImg")
    public R getUserImg(Integer userId){
        return R.ok().data("img",wjxServicelmpl.getUserImg(userId));
    }

    //删除房屋
    @RequestMapping("/deleteFangwu")
    public R deleteFangwu(Integer middleId){
        try {
            wjxServicelmpl.deleteFangwu(middleId);
            return R.ok().code(2000);
        }catch (Exception e){
            e.printStackTrace();
            return R.error().code(5000);
        }
    }
    //查询停车缴费
    @RequestMapping("/findTCJF")
    public R findTCJF(Integer userId){
        System.out.println("-------:"+userId);
            List<VisitorsRecordWvo> recordWvos=wjxServicelmpl.findTCJF(userId);
        for (VisitorsRecordWvo recordWvo : recordWvos) {
            recordWvo.setMoney(recordWvo.getDuration()*25);
        }
        System.out.println("recordWvos:"+recordWvos);
        return  R.ok().data("data",recordWvos);
    }
    //开始缴费
    @RequestMapping("/WjxStartJF")
    public R WjxStartJF(@RequestBody VisitorsRecordWvo visitorsRecordWvo){
        try {
            wjxServicelmpl.WjxStartJF(visitorsRecordWvo);
            return R.ok().code(2000);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("缴费失败");
            return R.error().code(5000);
        }
    }





//    公共类提取我的房屋信息（小区、楼栋、楼层、门户号）
    public Object WjxFindDeaultState(Integer userId,Integer middleId){
        List<CellIntermediateWvo> t=wjxServicelmpl.WjxFindDeaultState(userId,middleId);
        //如果大于一条代表是集合
        if (t.size()>1){
            return t;
        }else {
            //否则是对象
            CellIntermediateWvo tt=new CellIntermediateWvo();
            for (CellIntermediateWvo cellIntermediateWvo : t) {
                tt=cellIntermediateWvo;
            }
            return tt;
        }
    }
}
